package com.liqi.frame.comm.service.impl;

import com.liqi.frame.comm.dao.GroupDao;
import com.liqi.frame.comm.dao.UserDao;
import com.liqi.frame.comm.dao.UserRoleDao;
import com.liqi.frame.comm.po.User;
import com.liqi.frame.comm.po.UserRole;
import com.liqi.frame.comm.service.UserService;
import com.liqi.frame.comm.util.IdUtil;
import com.liqi.frame.comm.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author liqi on 2017-05-26.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Override
    public User getByUsername(String username) {
        return userDao.getByUsername(username);
    }

//    public Set<String> getRoles(String username) {
//        return userDao.getRoles(username);
//    }
//
//    public Set<String> getPermissions(String username) {
//        return userDao.getPermissions(username);
//    }

    @Override
    public User login(String username, String pswd) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("username", username);
        map.put("pswd", pswd);
        User user = userDao.login(map);
        return user;
    }

    @Override
    public List<User> selectList(String fullname, String[] groupIds) {
        if (fullname!=null) {
            fullname = fullname.trim();
        }
        if (groupIds!=null && groupIds.length>0) {
            return userDao.selectList(fullname, groupIds);
        }
        return null;
    }

    @Override
    public List<User> getListByUserType(String userType) {
        return userDao.getListByUserType(userType);
    }

    @Override
    public List<User> getListByPhone(String phone) {
        return userDao.getListByPhone(phone);
    }

    @Override
    public User getUserById(String id) {
        return userDao.getUserById(id);
    }

    @Override
    public int insertUser(String id, String username, String pswd, int status, String name, String phone, String email,
                          String comment, Float orderId, String groupId, String groupName, String theme, String avatar, String userType) {
        pswd = PasswordUtil.encrypt(username, pswd);
        return userDao.insertObject(id,username,pswd,status,name,phone,email,comment,orderId, groupId, groupName, theme, avatar, userType);
    }

    @Override
    public int updateUser(String id, String username, String pswd, int status, String fullname, String phone, String email,
                          String comment, Float orderId, String groupId, String groupName, String theme, String avatar, String userType) {
        if (pswd!=null && !"".equals(pswd)) {
            pswd = PasswordUtil.encrypt(username, pswd);
        }
        return userDao.updateObject(id,username,pswd,status,fullname,phone,email,comment,orderId, groupId, groupName, theme, avatar, userType);
    }

    @Override
    public int updateUserTheme(String userId, String theme) {
        if (userId != null && theme != null && !"".equals(userId) && !"".equals(theme)) {
            return userDao.updateUserTheme(userId, theme);
        }
        return 0;
    }

    @Override
    public int deleteUser(String id) {
        return userDao.deleteObjectById(id);
    }

    @Override
    public int saveUserAndRoles(String userId, String[] roleIds) {
        int resultCount = 0;
        List<UserRole> userRoleList = userRoleDao.getUserAndRolesByUserId(userId);
        if (userRoleList.size()>0) {
            String[] userRoleIdsForDel = new String[userRoleList.size()];

            for (int i = 0; i < userRoleList.size(); i++) {
                userRoleIdsForDel[i] = userRoleList.get(i).getId();
            }
            // 删除原有用户和角色的关联数据
            resultCount = userRoleDao.deleteUserAndRolesByIds(userRoleIdsForDel);
        }

        if (roleIds==null) {
            return resultCount;
        }
        // 添加新的角色和菜单的关联数据
        List<UserRole> userRolesForAdd = new ArrayList<>();
        for (int j = 0; j < roleIds.length; j++) {
            if (userId!=null && !"".equals(userId.trim()) && roleIds[j]!=null && !"".equals(roleIds[j].trim())) {
                UserRole userRole = new UserRole();
                userRole.setId(IdUtil.getId() + "");
                userRole.setUserId(userId);
                userRole.setRoleId(roleIds[j]);
                userRolesForAdd.add(userRole);
            }
        }
        if (userRolesForAdd.size()>0) {
            resultCount = userRoleDao.insertUserAndRoles(userRolesForAdd);
        }

        return resultCount;
    }

    @Override
    public boolean verifyUsernamePassword(String username, String password) {
        if (username==null || Objects.equals("", username) || password==null || Objects.equals("", password)) {
            return false;
        }

        password = PasswordUtil.encrypt(username, password);
        User user = userDao.verifyUsernamePassword(username, password);
        if (user!=null && user.getId()!=null && !Objects.equals("", user.getId())) {
            return true;
        }
        return false;
    }

    @Override
    public int modifyPassword(String username, String password) {
        int resultCount = 0;
        if (username==null || Objects.equals("", username) || password==null || Objects.equals("", password)) {
            return resultCount;
        }
        password = PasswordUtil.encrypt(username, password);
        resultCount = userDao.updatePassword(username, password);
        return resultCount;
    }

    @Override
    public int modifyPassword(String username, String oldPassword, String newPassword) {
        int resultCount = 0;
        if (this.verifyUsernamePassword(username, oldPassword)) {
            resultCount = this.modifyPassword(username, newPassword);
        }
        return resultCount;
    }

    @Override
    public List<User> getUserListByGroupId(String groupId) {
        return userDao.getUserListByGroupId(groupId);
    }

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private GroupDao groupDao;
}
